import api from '../../../services/api'
import {defineStore} from "pinia";
import {Pagination, Record, Sorting, Filters} from "../types";


const recordList = async (options: Partial<Sorting> & Pagination & Filters,) => {
  try {
    const res = await api.post('/admin/lottery/record/page/record', {
      "activityId": options.activityId,
      "prizeId": options.prizeId,
      "pageNumber": 0,
      "pageSize": 10
    })
    return {
      data: res.data.data.content,
      pagination: {
        page: res.data.data.pageable.pageNumber,
        perPage: options.perPage,
        total: res.data.data.totalElements,
      },
    }
  } catch (err) {
    console.error(err);
  }
  return {
    data: [],
    pagination: {
      page: 0,
      perPage: 0,
      total: 0,
    },
  };
};

export const useRecordStore = defineStore('record', {
  state: () => {
    return {
      items: [] as Record[],
      pagination: {
        page: 1,
        perPage: 10,
        total: 0,
      } as Pagination,
    }
  },

  actions: {
    async getPage(options: {sorting?: Sorting; pagination: Pagination; filters?: Partial<Filters> }) {
      const { data, pagination } = await recordList({
        ...options.filters,
        ...options.sorting,
        ...options.pagination,
      })
      this.items = data
      this.pagination = pagination
    }
  }
});
